package com.well.menjin.util;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.util.StringUtils;

/**
 * 
* @ClassName: JsonBuilder 
* @Description: Json工具类 
* @author 姜俊健 
* @date 2016年6月4日 下午1:20:58 
*
 */
public class JsonBuilder {
	/**
	 * 日志记录类
	 */
	public static Logger LOG = Logger.getLogger(JsonBuilder.class.getName());

	/**
	 * 得到JsonBuilder实例化对象
	 * 
	 * @return
	 */
	public static JsonBuilder getInstance() {
		return JsonHolder.JSON_BUILDER;
	}

	/**
	 * 静态内部类
	 * 
	 * @author zhangshuaipeng
	 * 
	 */
	private static class JsonHolder {
		private static final JsonBuilder JSON_BUILDER = new JsonBuilder();
		private static ObjectMapper mapper = new ObjectMapper();
	}

	/**
	 * 将一个数据实体解析成Json数据格式
	 * 
	 * @param obj
	 * @return
	 */
	public String toJson(Object obj) {
		try {
			return JsonHolder.mapper.writeValueAsString(obj);
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 将一个Json字符串封装为指定类型对象
	 * 
	 * @param json
	 * @param c
	 * @return
	 */
	public Object fromJson(String json, Class<?> c) {
		json = cleanJson(json);
		try {
			Object obj = JsonHolder.mapper.readValue(json, c);
			return obj;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 将一个JsonArray数据转换成一个List的键值对 [{name:'zsp',value:1},{name:'zsp',value:2}]
	 * 
	 * @param json
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<Map> fromJsonArray(String json) {
		json = cleanJson(json);
		List<Map> dataList = (List<Map>) fromJson(json, ArrayList.class);

		return dataList;
	}

	/**
	 * 为操作成功返回Json
	 * 
	 * @param strData
	 * @return
	 */
	public String returnSuccessJson(String strData) {
		StringBuffer returnJson = new StringBuffer("{ success : true , obj : '");
		returnJson.append(strData);
		returnJson.append("'}");
		return returnJson.toString();
	}

	public String returnSuccessmsgJson(String strData) {
		StringBuffer returnJson = new StringBuffer("{ success : true , msg : ");
		returnJson.append(strData);
		returnJson.append("}");
		return returnJson.toString();
	}

	/**
	 * 为操作失败返回Json
	 * 
	 * @param strData
	 * @return
	 */
	public String returnFailureJson(String strData) {
		StringBuffer returnJson = new StringBuffer(
				"{ success : false , obj : '");
		returnJson.append(strData);
		returnJson.append("'}");
		return returnJson.toString();
	}

	public String returnFailuremsgJson(String strData) {
		StringBuffer returnJson = new StringBuffer("{ success : false , msg : ");
		returnJson.append(strData);
		returnJson.append("}");
		return returnJson.toString();
	}
	
	public String getRequestResult(Long resultCode,String strData){
		StringBuffer returnJson = new StringBuffer("{ \"resultCode\" : "+resultCode+" , \"returnObject\" : ");
		returnJson.append(strData);
		returnJson.append(" }");
		return returnJson.toString();
		
	}

	/**
	 * 
	 * @param jsonData
	 * @return
	 */
	public String toGridJSon(Long count, String jsonData) {
		StringBuffer pageJson = new StringBuffer("{totalCount:" + count + ","
				+ "rows" + ":[");
		pageJson.append(jsonData);
		pageJson.append("]");
		return pageJson.toString();
	}

	/**
	 * 为分页列表提供Json封装
	 * 
	 * @param count
	 *            记录总数
	 * @param records
	 *            实体列表
	 * @param listJson
	 *            在json生成时需要排除的属性名称
	 * @return
	 */
	public String buildObjListToJson(Long count,
			Collection<? extends Object> records, boolean listJson) {
		try {
			StringBuffer pageJson = null;
			// 判断是否展示list的数据
			if (listJson) {
				pageJson = new StringBuffer("{totalCount:" + count + ","
						+ "rows" + ":");
			} else {
				pageJson = new StringBuffer("");
			}
			// 构建集合的json数据
			StringWriter w = new StringWriter();
			JsonHolder.mapper.writeValue(w, records);
			pageJson.append(w);
			w.close();

			if (listJson) {
				pageJson.append("}");
			} else {
				pageJson.append("");
			}
			return pageJson.toString();
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	/**
	 * 格式化Json
	 * 
	 * @param json
	 * @return
	 */
	public String cleanJson(String json) {
		if (false == StringUtils.isEmpty(json)) {
			return json.replaceAll("\n", "");
		} else {
			return "";
		}
	}

	public static String[] fromjsontosql(String json) {
		return null;
	}

}
